/**
 * @author 徐楠
 * @date 2022/3/20 14:08
 * @version 1.0
 */

package com.xunan.likou;

public class PictureSmoother {
    public static void main(String[] args) {
        int[][] img = {{1, 1, 1}, {1, 0, 1}, {1, 1, 1}};
        int[][] ints = imageSmootherPlus(img);
        for (int[] anInt : ints) {
            for (int i : anInt) {
                System.out.println(i);
            }
        }

    }

    public static int[][] imageSmootherPlus(int[][] img) {
        int x = img.length;
        int y = img[0].length;
        int[][] answer = new int[x][y];

        for (int i = 0; i < x; i++) {
            for (int j = 0; j < img[0].length; j++) {
                int sum = img[i][j];
                int count = 1;
                if (i - 1 >= 0) {
                    sum += img[i - 1][j];
                    count++;
                }
                if (i + 1 < x) {
                    sum += img[i + 1][j];
                    count++;
                }
                if (j - 1 >= 0) {
                    sum += img[i][j - 1];
                    count++;
                }
                if (j + 1 < y) {
                    sum += img[i][j + 1];
                    count++;
                }
                if (i - 1 >= 0 && j - 1 >= 0) {
                    sum += img[i - 1][j - 1];
                    count++;
                }
                if (i - 1 >= 0 && j + 1 < y) {
                    sum += img[i - 1][j + 1];
                    count++;
                }
                if (i + 1 < x && j - 1 >= 0) {
                    sum += img[i + 1][j - 1];
                    count++;
                }
                if (i + 1 < x && j + 1 < y) {
                    sum += img[i + 1][j + 1];
                    count++;
                }
                answer[i][j] = (int) Math.floor(sum / count);
            }
        }
        return answer;
    }

}
